package com.hc.model.utils;

import java.util.ArrayList;

import com.hc.DTO.AnimalCategoryMasterDTO;
import com.hc.DTO.AnimalDTO;
import com.hc.common.ActionEvent;
import com.hc.constants.Constants;
import com.hc.controller.AnimalCategoryMasterController;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class AnimalCategoryMasterModel {
	public static AnimalCategoryMasterModel instance;

	public static AnimalCategoryMasterModel getInstance() {
		if (instance == null)
			instance = new AnimalCategoryMasterModel();
		return instance;
	}

	public void handleControllerEvent(ActionEvent e) {

		switch (e.action) {
		case Constants.GET_ALL_CATEGORY:
			ArrayList<AnimalCategoryMasterDTO> arrData = getCategory((Context) e.sender);
			e.viewData = arrData;
			AnimalCategoryMasterController.getInstance()
					.handleModelViewEvent(e);
			break;

		default:
			break;
		}
	}

	ArrayList<AnimalCategoryMasterDTO> getCategory(Context context) {
		ArrayList<AnimalCategoryMasterDTO> arr = new ArrayList<AnimalCategoryMasterDTO>();
		String sql = "SELECT * FROM category ORDER BY OrderNo";
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			AnimalCategoryMasterDTO dto = new AnimalCategoryMasterDTO();
			dto.setId(c.getInt(c.getColumnIndex("Id")));
			dto.setAnimalCategory(c.getString(c
					.getColumnIndex("AnimalCategory")));
			dto.setOrderNo(c.getInt(c.getColumnIndex("OrderNo")));
			dto.setUrlImages(c.getString(c.getColumnIndex("UrlImages")));
			arr.add(dto);
		}
		c.close();
		db.close();

		return arr;
	}

	public boolean getIdCategory(Context context, int id) {
		boolean flag = false;
		String sql = "SELECT * FROM category where Id =" + id;
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();

		return flag;
	}

	public ArrayList<AnimalCategoryMasterDTO> getArrayId(Context context) {
		ArrayList<AnimalCategoryMasterDTO> arr = new ArrayList<AnimalCategoryMasterDTO>();
		String sql = "SELECT * FROM category order by Id";
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			AnimalCategoryMasterDTO dto = new AnimalCategoryMasterDTO();
			dto.setId(c.getInt(c.getColumnIndex("Id")));
			arr.add(dto);
		}
		c.close();
		db.close();

		return arr;
	}

	public boolean insertCategory(AnimalCategoryMasterDTO dto, Context context) {
		boolean flag = false;
		String DATABASE_TABLE = "category";

		ContentValues values = new ContentValues();
		values.put("Id", dto.getId());
		values.put("OrderNo", dto.getOrderNo());
		values.put("UrlImages", dto.getUrlImages());
		values.put("AnimalCategory", dto.getAnimalCategory());

		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
		db.close();

		return flag;
	}

	public boolean updateCategory(AnimalCategoryMasterDTO dto, Context context) {
		boolean flag = false;
		// String sql = "UPDATE changelog SET Id=" + id;
		DBHelper db = new DBHelper(context);
		String where = "Id=" + dto.getId() + "";
		ContentValues values = new ContentValues();
		values.put("OrderNo", dto.getOrderNo());
		values.put("UrlImages", dto.getUrlImages());
		values.put("AnimalCategory", dto.getAnimalCategory());
		flag = db.ExcuteUpdate(null, "category", values, where, null);
		db.close();
		return flag;
	}

	public void deleteAnimal(Context context) {
		String sql = "1=" + 1;
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = "category";
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
